package com.yz.xyt.componet;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.yz.xyt.domain.Staff;
import com.yz.xyt.domain.vo.StaffImportVo;
import com.yz.xyt.service.IDepartmentService;
import com.yz.xyt.service.IStaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StaffListener extends AnalysisEventListener<StaffImportVo> {

    private static final Logger LOGGER = LoggerFactory.getLogger(StaffListener.class);

    private static final Integer COUNT = 50;

    private List<StaffImportVo> staffList = new ArrayList<>();

    @Autowired
    IStaffService staffService;

    @Override
    public void invoke(StaffImportVo staff, AnalysisContext analysisContext) {
        LOGGER.info("解析了一条教职工记录，" + staff.toString());
        staffList.add(staff);

        if(staffList.size() == COUNT){
            List<Staff> staffs = staffService.convertStaff(staffList);
            if (!staffs.isEmpty()){
                staffService.syncAddStaff(staffs);
            }
            staffList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        List<Staff> staffs = staffService.convertStaff(staffList);
        if (!staffs.isEmpty()){
            staffService.syncAddStaff(staffs);
        }
        LOGGER.info("教职工数据导入完成");
        staffList.clear();
    }
}
